package com.hilton.tigershark.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.webkit.HttpAuthHandler;
import android.widget.EditText;
import android.widget.TextView;

import com.hilton.tigershark.R;
import com.hilton.tigershark.TabControl;

public class HttpAuthenticationManager {
    private final Activity mActivity;
    private final LayoutInflater mLayoutInflater;
    private final TabControl mTabControl;
    private HttpAuthHandler mHttpAuthHandler;
    private AlertDialog mHttpAuthDialog;
    private EditText mUsernameEdit;
    private EditText mPasswordEdit;
    private String mHttpHost;
    private String mHttpRealm;
    private String mDialogTitle;
    private View mCustomLayout;
    
    public HttpAuthenticationManager(Activity a, TabControl tc) {
        mActivity = a;
        mLayoutInflater = LayoutInflater.from(a);
        mTabControl = tc;
    }
    
    private void initLayout() {
        mCustomLayout = mLayoutInflater.inflate(R.layout.http_authentication,
                null);
        mUsernameEdit = (EditText) mCustomLayout.findViewById(R.id.username_edit);
        mPasswordEdit = (EditText) mCustomLayout.findViewById(R.id.password_edit);
    }
    
    public void showHttpAuthDialog(HttpAuthHandler handler, String host, String realm) {
        initLayout();
        init(handler, host, realm);
        mHttpAuthDialog = showDialogAndFocusOn(R.id.username_edit);
    }
    
    private void init(HttpAuthHandler handler, String host, String realm) {
        mHttpAuthHandler = handler;
        mHttpHost = host;
        mHttpRealm = realm;
        mDialogTitle = mActivity.getString(R.string.sign_in_to, host, realm);
    }
    
    public boolean hasDialogShowing() {
        return mHttpAuthDialog != null && mHttpAuthDialog.isShowing();
    }
    
    public void reshowDialog() {
        dismissDialog();
        relayoutDialog();
    }
    
    private void dismissDialog() {
        mHttpAuthDialog.dismiss();
    }
    
    private void relayoutDialog() {
        initLayout();
        String name = ((TextView) mHttpAuthDialog
                .findViewById(R.id.username_edit)).getText().toString();
        String password = ((TextView) mHttpAuthDialog
                .findViewById(R.id.password_edit)).getText().toString();
        int focusId = mHttpAuthDialog.getCurrentFocus().getId();
        if (!TextUtils.isEmpty(name)) {
            mUsernameEdit.setText(name);
        }
        if (!TextUtils.isEmpty(password)) {
            mPasswordEdit.setText(password);
        }
        disposeDialog();
        mHttpAuthDialog = showDialogAndFocusOn(focusId);
    }

    private AlertDialog showDialogAndFocusOn(int focusId) {
        final AlertDialog dialog = createHttpAuthDialog();
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        dialog.show();
        dialog.findViewById(focusId).requestFocus();
        return dialog;
    }
    
    private AlertDialog createHttpAuthDialog() {
        final AlertDialog dialog = new AlertDialog.Builder(mActivity)
            .setTitle(mDialogTitle)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setView(mCustomLayout)
            .setPositiveButton(R.string.action, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    approveAuthentication();
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    declineAuthenticiation();
                }
            })
            .setOnCancelListener(new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface dialog) {
                    declineAuthenticiation();
                }
            })
            .create();
        return dialog;
    }
    
    private void approveAuthentication() {
        final String nm = mUsernameEdit.getText().toString();
        final String pw = mPasswordEdit.getText().toString();
        mTabControl.setHttpAuthUsernamePassword(mHttpHost, mHttpRealm, nm, pw);
        mHttpAuthHandler.proceed(nm, pw);
        disposeDialog();
    }
    
    private void declineAuthenticiation() {
        mHttpAuthHandler.cancel();
        mTabControl.resetTitleAndRevertLockIcon();
        disposeDialog();
    }
    
    private void disposeDialog() {
        mHttpAuthDialog = null;
    }
}